<template>
  <div>
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryParams.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="goodsInfo" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="100px"></el-table-column>
        <el-table-column label="是否付款" width="100px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay === '0'" type="warning">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100px"></el-table-column>
        <el-table-column label="下单时间" width="150px">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" class="el-icon-edit"  @click="editAddr"></el-button>
            <el-button type="success" size="mini" class="el-icon-location-outline" @click="progressQuery"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :total="total" @size-change="sizeChange" @current-change="currentChange" :current-page="queryParams.pagenum" :page-sizes="pageSizes" :page-size="queryParams.pagesize"  layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </el-card>
    <el-dialog title="修改地址" width="50%" :visible.sync="isEditAddrDialogVisible" :close-on-click-modal="false" @close="resetFormData">
      <el-form :model="addrInfo" ref="addrFormRef" :rules="addrFormRules" label-width="100px">
        <el-form-item label="省市区/县" prop="mainAddress">
          <el-cascader :options="cityData" v-model="addrInfo.mainAddress"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress">
          <el-input v-model="addrInfo.detailedAddress"></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer" slot="footer">
        <el-button @click="isEditAddrDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="isEditAddrDialogVisible = false">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <el-timeline :reverse="reverse" type="primary">
          <el-timeline-item type="warning" v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './privateScript/citydata.js'
export default {
  data() {
    return {
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 6,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      goodsInfo: [],
      total: 0,
      pageSizes: [6, 7, 8, 9],
      isEditAddrDialogVisible: false,
      addrInfo: {
        mainAddress: [],
        detailedAddress: ''
      },
      addrFormRules: {
        mainAddress: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        detailedAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      progressDialogVisible: false,
      progressInfo: [],
      reverse: false
    }
  },
  created() {
    this.getOrders()
  },
  methods: {
    async getOrders() {
      console.log(this.queryParams)
      const { data: res } = await this.$http.get('orders', { params: this.queryParams })
      if(res.meta.status !== 200) return this.$message({ type: 'error', message: '获取订单信息失败', duration: 2000 })
      this.goodsInfo = res.data.goods
      this.total = res.data.total
    },
    sizeChange(newPageSize) {
      this.queryParams.pagesize = newPageSize
      this.getOrders()
    },
    currentChange(newPageNum) {
      this.queryParams.pagenum = newPageNum
      this.getOrders()
    },
    handleSearch() {
      if(this.queryParams.query.trim() === '') {
        this.queryParams.pagenum = 1
      }
      this.getOrders()
    },
    editAddr() {
      this.isEditAddrDialogVisible = true
    },
    resetFormData() {
      this.$refs.addrFormRef.resetFields()
    },
    async progressQuery() {
      this.progressInfo = []
      this.progressDialogVisible = true
      const { data: res } = await this.$http.get('kuaidi/' + 1106975712662)
      if(res.meta.status !== 200) return this.$message({ type: 'error', message: res.meta.msg, duration: 2000 })
      this.progressInfo = res.data
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/ .el-input__inner{
    height: 39px;
  }
  .el-cascader{
    width: 100%;
  }
</style>
